from typing import Any

from aiogram import F, types
from aiogram_dialog import DialogManager, Window
from aiogram_dialog.widgets.kbd import Back, Cancel, ScrollingGroup, Select
from aiogram_dialog.widgets.text import Format

from app.bot.dialog.dialog_states import SelectProxySG
from app.bot.dialog.getters.categories import categories_getter
from app.bot.dialog.widgets import LocaleText


async def set_category_id(
    call: types.CallbackQuery, widget: Any, manager: DialogManager, category_id: str
):
    manager.dialog_data["category_id"] = int(category_id)
    await manager.switch_to(SelectProxySG.list_of_sites)


categories_window = Window(
    LocaleText("select-category"),
    ScrollingGroup(
        Select(
            Format("{item.name}"),
            "catselsel",
            lambda category: category.id,
            "categories",
            on_click=set_category_id,
        ),
        width=2,
        height=4,
        id="catsel",
        when=F["user_language"] == "ru",
    ),
    ScrollingGroup(
        Select(
            Format("{item.name_eng}"),
            "catselsel",
            lambda category: category.id,
            "categories",
            on_click=set_category_id,
        ),
        width=2,
        height=4,
        id="catsel",
        when=F["user_language"] == "en",
    ),
    Back(LocaleText("back-btn")),
    Cancel(LocaleText("cancel-btn")),
    state=SelectProxySG.list_of_categories,
    getter=categories_getter,
)
